<template>
  <el-dialog v-model="visible" :close-on-click-modal="false" :close-on-press-escape="false" z-index="999">
    <div style="display: flex; justify-content: space-between;margin-bottom: 20px;padding-top: 10px">
      <div style="display: flex; align-items: center">
        <div class="p-flex">
          <span style="width: 60px">搜索</span>
          <el-input v-model="tableAddr" maxlength="44" @keyup.enter="inputSearch()"></el-input>
        </div>
        <div class="p-flex">
          <span style="width: 100px">轮播间隔（秒）</span>
          <el-input-number v-model="secondNum" :precision="0" :step="1" :min="3" :max="3600"/>
        </div>
        <div class="p-flex">
          已选{{ checked.length }}
          <!--          <el-checkbox v-model="checkedlook">只看已选（{{ checked.length }}）</el-checkbox>-->
        </div>
      </div>
      <div>
        <el-button type="primary" @click="startVideoPlay()">开始轮播</el-button>
      </div>
    </div>

    <el-table :data="crmListall" row-key="id" @select="selectChange" ref="multipleTable" @select-all="selectAllChange"
              @selection-change="handleSelectionChange" height="400">
      <el-table-column type="selection" width="55"/>
      <el-table-column property="name" label="杆塔名称"/>
    </el-table>
  </el-dialog>
</template>

<script>
import baseService from "@/service/baseService";
import {ElMessage} from "element-plus";

export default {
  data() {
    return {
      tableData: [], //初始数据
      crmListall: [], //筛选展示数据
      checkAll: false,
      checked: [],
      tableAddr: "",
      secondNum: 5,
      checkedlook: false,
      cirblooun: false,
      setIntervalTd: null,
      visible: false
    }
  },
  watch: {
    checkedlook() {
      if (this.checkedlook) {
        this.crmListall = this.checked;
      } else {
        this.crmListall = this.tableData;
      }
    }
  },
  mounted() {
    // this.init();
  },
  methods: {
    init() {
      this.visible = true;
      this.$nextTick(() => {
        this.getMapList();
        console.log(this.checked)
        this.toggleSelection(this.checked);
      });
    },
    getMapList() {
      this.tableData = [];
      baseService.get("/KxStation/kxstation/getMapList").then((res) => {
        this.tableData = res.data;
        this.crmListall = this.tableData;
      });
    },
    // 数据回显
    toggleSelection(rows) {
      this.$nextTick(() => {
        setTimeout(() => {
          if (rows && this.crmListall.length != 0) {
            this.crmListall.forEach((row) => {
              rows.forEach((item) => {
                if (row.id == item.id) {
                  console.log(item.name)
                  this.$refs.multipleTable.toggleRowSelection(row, true)
                }
              })
            })
          } else {
            this.$refs.multipleTable.clearSelection();
          }
        }, 100);
      });
    },
    inputSearch() {
      this.crmListall = this.fuzzySearch(this.tableAddr, this.tableData);
    },
    fuzzySearch(keyword, data) {
      return data.filter((item) => item.addr.includes(keyword));
    },
    selectChange(selection) {
      this.checked = selection;
    },
    selectAllChange(selection) {
      this.checked = selection;
    },
    handleSelectionChange(selection) {
      //处理数据
      //。。。
      // checked.value = selection.map((item) => item.id);
    },
    startVideoPlay() {
      if (this.checked <= 0) {
        ElMessage.warning("请先选择轮播数据！");
        return;
      }
      const mapdata = this.checked.map(function (item) {
        return {
          ...item,
          type: "station"
        }
      });
      this.$emit('startMapPlay', {data: mapdata, time: this.secondNum});
    }
  }
}
</script>

<style scoped lang="scss">
.p-flex {
  display: flex;
  align-items: center;
  padding-right: 20px;
}
</style>
